package com.meixiang.activity.homes.service;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.adapter.service.SelectionTimeAdapter;
import com.meixiang.entity.SelectionTime;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.util.AbToastUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 时间选择
 */
public class SelectionTimeActivity extends BaseActivity {

    @Bind(R.id.recycler)
    RecyclerView recycler;
    private List<SelectionTime> data;
    private SelectionTimeAdapter adapter;

    private String date;
    private String beauticianId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_selection_time);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setRightTitle("时间选择", "确认", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                for (int i=0;i<adapter.getData().size();i++){
                    if (adapter.getData().get(i).isChecked()){
                        Intent intent=new Intent();
                        intent.putExtra("date",date);
                        intent.putExtra("time",adapter.getData().get(i).getTime());
                        intent.putExtra("beauticianId",beauticianId);
                        setResult(200, intent);
                        finish();
                        return;
                    }
                }
                if (adapter.getData().size()==0){
                    AbToastUtil.showToast(activity,"暂无可预约时间");
                }else{
                    AbToastUtil.showToast(activity,"请选择预约时间");
                }

            }
        });
    }

    @Override
    protected void initView() {
        recycler.setLayoutManager(new LinearLayoutManager(context));
        adapter=new SelectionTimeAdapter();
        recycler.setAdapter(adapter);

    }

    @Override
    protected void setData() {
        date=getIntent().getStringExtra("date");
        beauticianId=getIntent().getStringExtra("beauticianId");
        status.showLoading();
        getData();
    }
    private void getData(){
        HttpParams params=new HttpParams();
        params.put("goodsId", getIntent().getStringExtra("goodsId"));
        params.put("beauticianId",beauticianId);
        params.put("date",date);
        Log.e("params",params.toString());
        HttpUtils.post(Config.QUERY_FREE_TIME, params, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                JSONArray array=result.optJSONArray("list");

                if (array==null||array.length()==0){
                        status.showNoData("暂无可预约时间");
                    }else{
                        status.removeView();
                        data=new ArrayList<>();
                        for (int i=0;i<array.length();i++){
                            SelectionTime time=new SelectionTime(array.optString(i),false);
                            data.add(time);
                        }
                        Log.e("data",data.toString());
                        adapter.addData(data);
                    }

            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(context,message);
                status.showNoData(message, "重新加载", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        status.showLoading();
                        getData();
                    }
                });
            }
        });
    }

    @Override
    public void onClick(View v) {

    }
}
